package com.mapapp.flutter_mapapp.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.mapapp.flutter_mapapp.R;
import com.mapapp.flutter_mapapp.persenter.ResetPasswordPersenter;
import com.mapapp.flutter_mapapp.util.MyActivityManager;
import com.mapapp.flutter_mapapp.util.ToastUtils;
import com.mapapp.flutter_mapapp.view.ResetPasswordViewInterface;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ResetPasswordActivity extends ImmersiveBaseActivity implements OnClickListener , ResetPasswordViewInterface {
    private static final String TAG = "ResetPasswordActivity";
    @BindView(R.id.iv_reset_password_back)
    ImageView ivResetPasswordBack;
    @BindView(R.id.et_input_new_password)
    EditText etInputNewPassword;
    @BindView(R.id.et_ensure_new_password)
    EditText etEnsureNewPassword;
    @BindView(R.id.btn_reset_ensure)
    Button btnResetEnsure;
    @BindView(R.id.btn_reset_cancel)
    Button btnResetCancel;

    private String mTempToken;
    private String mPhone;
    private ResetPasswordPersenter mPersenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_password);
        ButterKnife.bind(this);

        mContext = this;
        init();
        mTempToken = getIntent().getStringExtra("tempToken");
        mPhone = getIntent().getStringExtra("phone");
        mPersenter = new ResetPasswordPersenter(this,this,lifecycleSubject);
    }

    private void init(){
        ivResetPasswordBack.setOnClickListener(this);
        btnResetEnsure.setOnClickListener(this);
        btnResetCancel.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_reset_password_back:
                setResult(112);
                MyActivityManager.getActivityManager().pop(this);
                break;
            case R.id.btn_reset_ensure:
                reset();
                break;
            case R.id.btn_reset_cancel:
                setResult(112);
                MyActivityManager.getActivityManager().pop(this);
                break;
        }
    }

    private void reset(){
        String inputPassword = etInputNewPassword.getText().toString().trim();
        String ensurePassword = etEnsureNewPassword.getText().toString().trim();
        if("".equals(inputPassword)){
            ToastUtils.showMessage(mContext, "请输入新密码");
            return;
        }
        if("".equals(ensurePassword)){
            ToastUtils.showMessage(mContext, "请输入确认新密码");
            return;
        }
        if(!inputPassword.equals(ensurePassword)){
            ToastUtils.showMessage(mContext, "新密码和确认密码不一致");
            return;
        }
        mPersenter.resetPassword(mPhone,mTempToken,ensurePassword);
    }

    @Override
    public void resetSuccess() {
        setResult(111);
        MyActivityManager.getActivityManager().pop(this);
    }
}
